﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Sgr.Domain.Entities;

namespace Sgr.Domain.Managers
{
    /// <summary>
    /// 树形结构聚合的领域服务基类接口
    /// </summary>
    /// <typeparam name="TEntity">实体类型</typeparam>
    /// <typeparam name="TPrimaryKey">主键类型</typeparam>
    public interface ITreeNodeManage<TEntity, TPrimaryKey> : IDomainManager
        where TEntity : class, IEntity<TPrimaryKey>, ITreeNode<TPrimaryKey>, IAggregateRoot
    {
        /// <summary>
        /// 移动节点（包括子节点）到新位置
        /// </summary>
        /// <param name="entity">要移动的节点</param>
        /// <param name="newParentId">新的父节点ID</param>
        /// <param name="maxLevel">最大层级限制</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>操作结果</returns>
        Task<DomainRuleCheckResult> MoveNodeAsync(TEntity entity, TPrimaryKey newParentId, int maxLevel = 10, CancellationToken cancellationToken = default);

        /// <summary>
        /// 移动节点（包括子节点）到新位置
        /// </summary>
        /// <param name="entityId">要移动的节点ID</param>
        /// <param name="newParentId">新的父节点ID</param>
        /// <param name="maxLevel">最大层级限制</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>操作结果</returns>
        Task<DomainRuleCheckResult> MoveNodeAsync(TPrimaryKey entityId, TPrimaryKey newParentId, int maxLevel = 10, CancellationToken cancellationToken = default);

        /// <summary>
        /// 验证移动操作是否有效
        /// </summary>
        /// <param name="sourceId">源节点ID</param>
        /// <param name="targetId">目标节点ID</param>
        /// <param name="maxLevel">最大层级限制</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>验证结果</returns>
        Task<DomainRuleCheckResult> ValidateMoveOperationAsync(TPrimaryKey sourceId, TPrimaryKey targetId, int maxLevel = 10, CancellationToken cancellationToken = default);

        /// <summary>
        /// 验证移动操作是否有效
        /// </summary>
        /// <param name="sourceEntity">源节点</param>
        /// <param name="targetId">目标节点ID</param>
        /// <param name="maxLevel">最大层级限制</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>验证结果</returns>
        Task<DomainRuleCheckResult> ValidateMoveOperationAsync(TEntity sourceEntity, TPrimaryKey targetId, int maxLevel = 10, CancellationToken cancellationToken = default);

        /// <summary>
        /// 删除节点及其子节点
        /// </summary>
        /// <param name="id">要删除的节点ID</param>
        /// <param name="isCascade">是否级联删除子节点</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>删除结果</returns>
        Task<bool> RemoveNodeAsync(
            TPrimaryKey id,
            bool isCascade = true,
            CancellationToken cancellationToken = default);

        /// <summary>
        /// 删除节点及其子节点
        /// </summary>
        /// <param name="entity">节点</param>
        /// <param name="isCascade">是否级联删除子节点</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>删除结果</returns>
        Task<bool> RemoveNodeAsync(
            TEntity entity,
            bool isCascade = true,
            CancellationToken cancellationToken = default);
    }
}